/* Login - Decompiled by JODE
 * Visit http://jode.sourceforge.net/
 */
import java.io.IOException;
import java.net.Socket;

public class Login {
	public static JString aClass3_457 = Class39_Sub5_Sub9.createJString(109, "Remove");
	public static JString aClass3_458;
	public static int anInt459 = 0;
	public static JString aClass3_460;
	public static Class19 aClass19_461;
	public static JString aClass3_462;
	public static JString aClass3_463 = Class39_Sub5_Sub9.createJString(115, "Please contact customer support)3");
	public static JString aClass3_464;
	public static Applet_Sub1 anApplet_Sub1_465;
	public static JString aClass3_466;
	public static Class18 aClass18_467;
	public static JString aClass3_468;
	public static JString aClass3_469;
	public static JString aClass3_470;
	public static int anInt471;
	public static Class40 aClass40_472;

	public static void method281(int i, int i_0_, int i_1_, boolean bool) {
		RSInterface class39_sub5_sub17 = Class62_Sub2.method1081(i_1_, i_0_, 0);
		if (class39_sub5_sub17 != null && class39_sub5_sub17.anObjectArray2099 != null)
			GameCharacter.method514(0, (class39_sub5_sub17.anObjectArray2099), null, 114, 0, class39_sub5_sub17, 0);
		ISAAC.aBoolean1100 = bool;
		Class31.anInt570 = i_0_;
		Class41.anInt776 = i;
		Class41.anInt775 = i_1_;
	}

	public static int method282(int i, byte i_2_, int i_3_, byte[] is) {
		int i_4_ = -1;
		for (int i_5_ = i_3_; i > i_5_; i_5_++)
			i_4_ = (Class39_Sub7.anIntArray1375[(i_4_ ^ is[i_5_]) & 0xff] ^ i_4_ >>> 8);
		i_4_ ^= 0xffffffff;
		return i_4_;
	}

	public static void method283(int i, int i_6_, int i_7_, byte i_8_, int i_9_, int i_10_, int i_11_, int i_12_, int i_13_) {
		if (RSSocket.method219(0, i_11_))
			Class39_Sub4.method460(-1, 0, i_12_, 0, i_9_, -1, i_7_, i_10_, i, (Class62_Sub1.interfaceCache[i_11_]), i_6_, i_13_);
	}

	public static void loadRegion(int i, boolean bool) {
		Class63.aBoolean1120 = bool;
		if (i != -519790717)
			aClass3_466 = null;
		if (!Class63.aBoolean1120) {
			int i_14_ = Class39_Sub5_Sub11.inputStream.readShortLE(10670);
			int i_15_ = Class39_Sub5_Sub11.inputStream.readShortLE(10670);
			int i_16_ = Class39_Sub5_Sub11.inputStream.readShortLEA((byte) 127);
			int i_17_ = Class39_Sub5_Sub11.inputStream.readByteA((byte) -98);
			int i_18_ = ((Class40.packetSize - Class39_Sub5_Sub11.inputStream.currentOffset) / 16);
			Class14.XTEAKeys = new int[i_18_][4];
			for (int i_19_ = 0; i_19_ < i_18_; i_19_++) {
				for (int i_20_ = 0; i_20_ < 4; i_20_++)
					Class14.XTEAKeys[i_19_][i_20_] = Class39_Sub5_Sub11.inputStream.readInt1();
			}
			int i_21_ = Class39_Sub5_Sub11.inputStream.readShortA(-1);
			Class65.anIntArray1132 = new int[i_18_];
			Class55.anIntArray1255 = new int[i_18_];
			Class7.aByteArrayArray104 = new byte[i_18_][];
			Class39_Sub5_Sub16.aByteArrayArray1989 = new byte[i_18_][];
			boolean bool_22_ = false;
			Class39_Sub5_Sub1.anIntArray1682 = new int[i_18_];
			i_18_ = 0;
			if ((i_14_ / 8 == 48 || i_14_ / 8 == 49) && i_21_ / 8 == 48)
				bool_22_ = true;
			if (i_14_ / 8 == 48 && i_21_ / 8 == 148)
				bool_22_ = true;
			for (int i_23_ = (i_14_ - 6) / 8; (i_14_ + 6) / 8 >= i_23_; i_23_++) {
				for (int i_24_ = (i_21_ - 6) / 8; i_24_ <= (i_21_ + 6) / 8; i_24_++) {
					int i_25_ = (i_23_ << 8) + i_24_;
					if (!bool_22_ || (i_24_ != 49 && i_24_ != 149 && i_24_ != 147 && i_23_ != 50 && (i_23_ != 49 || i_24_ != 47))) {
						Class65.anIntArray1132[i_18_] = i_25_;
						Class39_Sub5_Sub1.anIntArray1682[i_18_] = (RSSocket.aClass9_Sub1_312.method163((Class39_Sub5_Sub11.method708((new JString[] { Applet_Sub1.aClass3_4, Class57.method1007((byte) 71, i_23_), Class32.aClass3_593, Class57.method1007((byte) 71, i_24_) }), i + 519805286)), (byte) 30));
						Class55.anIntArray1255[i_18_] = (RSSocket.aClass9_Sub1_312.method163((Class39_Sub5_Sub11.method708((new JString[] { ObjectDef.aClass3_1946, Class57.method1007((byte) 71, i_23_), Class32.aClass3_593, Class57.method1007((byte) 71, i_24_) }), 14569)), (byte) 30));
						i_18_++;
					}
				}
			}
			Class39_Sub5_Sub3.method486(i_17_, i_21_, (byte) -113, i_16_, i_15_, i_14_);
		} else {
			Class39_Sub5_Sub11.inputStream.initBitAccess();
			for (int i_26_ = 0; i_26_ < 4; i_26_++) {
				for (int i_27_ = 0; i_27_ < 13; i_27_++) {
					for (int i_28_ = 0; i_28_ < 13; i_28_++) {
						int i_29_ = Class39_Sub5_Sub11.inputStream.readBits(1);
						if (i_29_ == 1)
							Class39_Sub5_Sub6.anIntArrayArrayArray1755[i_26_][i_27_][i_28_] = Class39_Sub5_Sub11.inputStream.readBits(26);
						else
							Class39_Sub5_Sub6.anIntArrayArrayArray1755[i_26_][i_27_][i_28_] = -1;
					}
				}
			}
			Class39_Sub5_Sub11.inputStream.finishBitAccess();
			int i_30_ = ((-Class39_Sub5_Sub11.inputStream.currentOffset + Class40.packetSize) / 16);
			Class14.XTEAKeys = new int[i_30_][4];
			for (int i_31_ = 0; i_30_ > i_31_; i_31_++) {
				for (int i_32_ = 0; i_32_ < 4; i_32_++)
					Class14.XTEAKeys[i_31_][i_32_] = Class39_Sub5_Sub11.inputStream.readInt();
			}
			int i_33_ = Class39_Sub5_Sub11.inputStream.readShortLEA((byte) 122);
			int i_34_ = Class39_Sub5_Sub11.inputStream.readShortLE(i + 519801387);
			int i_35_ = Class39_Sub5_Sub11.inputStream.readShortLEA((byte) 123);
			int i_36_ = Class39_Sub5_Sub11.inputStream.readShortLE(i + 519801387);
			int i_37_ = Class39_Sub5_Sub11.inputStream.readByteC((byte) 16);
			Class39_Sub5_Sub1.anIntArray1682 = new int[i_30_];
			Class65.anIntArray1132 = new int[i_30_];
			Class7.aByteArrayArray104 = new byte[i_30_][];
			Class55.anIntArray1255 = new int[i_30_];
			Class39_Sub5_Sub16.aByteArrayArray1989 = new byte[i_30_][];
			i_30_ = 0;
			for (int i_38_ = 0; i_38_ < 4; i_38_++) {
				for (int i_39_ = 0; i_39_ < 13; i_39_++) {
					for (int i_40_ = 0; i_40_ < 13; i_40_++) {
						int i_41_ = (Class39_Sub5_Sub6.anIntArrayArrayArray1755[i_38_][i_39_][i_40_]);
						if (i_41_ != -1) {
							int i_42_ = (i_41_ & 0x3ff9) >> 3;
							int i_43_ = (i_41_ & 0xfff459) >> 14;
							int i_44_ = i_42_ / 8 + (i_43_ / 8 << 8);
							for (int i_45_ = 0; i_45_ < i_30_; i_45_++) {
								if (Class65.anIntArray1132[i_45_] == i_44_) {
									i_44_ = -1;
									break;
								}
							}
							if (i_44_ != -1) {
								Class65.anIntArray1132[i_30_] = i_44_;
								int i_46_ = i_44_ >> 8 & 0xff;
								int i_47_ = i_44_ & 0xff;
								Class39_Sub5_Sub1.anIntArray1682[i_30_] = (RSSocket.aClass9_Sub1_312.method163((Class39_Sub5_Sub11.method708((new JString[] { Applet_Sub1.aClass3_4, Class57.method1007((byte) 71, i_46_), Class32.aClass3_593, Class57.method1007((byte) 71, i_47_) }), Class39_Sub5_Sub4_Sub4_Sub1.method522(i, -519788694))), (byte) 30));
								Class55.anIntArray1255[i_30_] = (RSSocket.aClass9_Sub1_312.method163((Class39_Sub5_Sub11.method708((new JString[] { ObjectDef.aClass3_1946, Class57.method1007((byte) 71, i_46_), Class32.aClass3_593, Class57.method1007((byte) 71, i_47_) }), 14569)), (byte) 30));
								i_30_++;
							}
						}
					}
				}
			}
			Class39_Sub5_Sub3.method486(i_37_, i_36_, (byte) -127, i_35_, i_33_, i_34_);
		}
	}

	public static void performLogin(int i) {
		try {
			if (Stream.loginStage == 0) {
				if (Class37.activeSocket != null) {
					Class37.activeSocket.killThread(i - 2148);
					Class37.activeSocket = null;
				}
				Stream.loginStage = 1;
				ObjectDef.aClass56_1963 = null;
				RSInterface.aBoolean2116 = false;
				Class15.anInt280 = 0;
			}
			if (Stream.loginStage == 1) {
				if (ObjectDef.aClass56_1963 == null)
					ObjectDef.aClass56_1963 = Class39_Sub5_Sub9.aClass21_1802.method255(Class19.anInt363, i + 14);
				if (ObjectDef.aClass56_1963.state == 2)
					throw new IOException();
				if (ObjectDef.aClass56_1963.state == 1) {
					Class37.activeSocket = new RSSocket((Socket) (ObjectDef.aClass56_1963.resource), Class39_Sub5_Sub9.aClass21_1802);
					Stream.loginStage = 2;
					ObjectDef.aClass56_1963 = null;
				}
			}
			if (Stream.loginStage == 2) {
				long l = (client.aLong1278 = Tile.playerName.toInt64());
				PacketStream.outputStream.currentOffset = 0;
				PacketStream.outputStream.writeByte((byte) -28, 14);
				int i_48_ = (int) (l >> 16 & 0x1fL);
				PacketStream.outputStream.writeByte((byte) -28, i_48_);
				Class37.activeSocket.write(-1, (PacketStream.outputStream.buffer), 2, 0);
				Class39_Sub5_Sub11.inputStream.currentOffset = 0;
				Stream.loginStage = 3;
			}
			if (Stream.loginStage == 3) {
				int i_49_ = Class37.activeSocket.read();
				if (i_49_ != 0) {
					Class10.method180(i_49_, 24);
					return;
				}
				Stream.loginStage = 4;
				Class39_Sub5_Sub11.inputStream.currentOffset = 0;
			}
			if (Stream.loginStage == 4) {
				if (Class39_Sub5_Sub11.inputStream.currentOffset < 8) {
					int i_50_ = Class37.activeSocket.available();
					if ((-Class39_Sub5_Sub11.inputStream.currentOffset + 8) < i_50_)
						i_50_ = 8 - (Class39_Sub5_Sub11.inputStream.currentOffset);
					if (i_50_ > 0) {
						Class37.activeSocket.read(i_50_, (Class39_Sub5_Sub11.inputStream.buffer), (Class39_Sub5_Sub11.inputStream.currentOffset));
						Class39_Sub5_Sub11.inputStream.currentOffset += i_50_;
					}
				}
				if (Class39_Sub5_Sub11.inputStream.currentOffset == 8) {
					Class39_Sub5_Sub11.inputStream.currentOffset = 0;
					Class35.serverSessionKey = Class39_Sub5_Sub11.inputStream.readLong();
					Stream.loginStage = 5;
				}
			}
			if (Stream.loginStage == 5) {
				PacketStream.outputStream.currentOffset = 0;
				int[] keys = new int[4];
				keys[0] = (int) (Math.random() * 9.9999999E7);
				keys[1] = (int) (Math.random() * 9.9999999E7);
				keys[3] = (int) Class35.serverSessionKey;
				keys[2] = (int) (Class35.serverSessionKey >> 32);
				PacketStream.outputStream.writeByte((byte) -28, 10);
				PacketStream.outputStream.writeInt(keys[0]);
				PacketStream.outputStream.writeInt(keys[1]);
				PacketStream.outputStream.writeInt(keys[2]);
				PacketStream.outputStream.writeInt(keys[3]);
				PacketStream.outputStream.writeInt(Class39_Sub5_Sub9.aClass21_1802.anInt410);
				PacketStream.outputStream.writeLong(true, Tile.playerName.toInt64());
				PacketStream.outputStream.writeString((byte) 102, Tile.playerPassword);
				// PacketStream.outputStream.method828(Class2.aBigInteger47,
				// RSSocket.aBigInteger292, -17694);
				JString.loginOutputStream.currentOffset = 0;
				if (Class31.anInt563 == 40)
					JString.loginOutputStream.writeByte((byte) -28, 18);
				else
					JString.loginOutputStream.writeByte((byte) -28, 16);
				JString.loginOutputStream.writeByte((byte) -28, PacketStream.outputStream.currentOffset + 61);
				JString.loginOutputStream.writeInt(443);
				JString.loginOutputStream.writeByte((byte) -28, !Class45.lowDetail ? 0 : 1);
				JString.loginOutputStream.writeInt((Stream.aClass9_Sub1_1358.anInt143));
				JString.loginOutputStream.writeInt(Class15.aClass9_Sub1_275.anInt143);
				JString.loginOutputStream.writeInt(Class67.aClass9_Sub1_1170.anInt143);
				JString.loginOutputStream.writeInt(Node.aClass9_Sub1_738.anInt143);
				JString.loginOutputStream.writeInt(Node.aClass9_Sub1_732.anInt143);
				JString.loginOutputStream.writeInt(RSSocket.aClass9_Sub1_312.anInt143);
				JString.loginOutputStream.writeInt(Class39_Sub5_Sub4_Sub1.aClass9_Sub1_2177.anInt143);
				JString.loginOutputStream.writeInt(Class45.aClass9_Sub1_870.anInt143);
				JString.loginOutputStream.writeInt(CollisionMap.aClass9_Sub1_520.anInt143);
				JString.loginOutputStream.writeInt((RSInterface.aClass9_Sub1_2060.anInt143));
				JString.loginOutputStream.writeInt(Class66.aClass9_Sub1_1155.anInt143);
				JString.loginOutputStream.writeInt(Class36.aClass9_Sub1_650.anInt143);
				JString.loginOutputStream.writeInt(Class33.aClass9_Sub1_603.anInt143);
				JString.loginOutputStream.writeInt(Class55.aClass9_Sub1_1242.anInt143);
				JString.loginOutputStream.writeBytes(0, PacketStream.outputStream.buffer, PacketStream.outputStream.currentOffset);
				Class37.activeSocket.write(-1, JString.loginOutputStream.buffer, JString.loginOutputStream.currentOffset, 0);
				PacketStream.outputStream.initializeISAAC(keys, -30242);
				for (int i_51_ = 0; i_51_ < 4; i_51_++)
					keys[i_51_] += 50;
				Class39_Sub5_Sub11.inputStream.initializeISAAC(keys, i ^ 0x7628);
				Stream.loginStage = 6;
			}
			if (Stream.loginStage == 6 && Class37.activeSocket.available() > 0) {
				int i_52_ = Class37.activeSocket.read();
				if (i_52_ != 21 || Class31.anInt563 != 20) {
					if (i_52_ == 2)
						Stream.loginStage = 9;
					else {
						if (i_52_ == 15 && Class31.anInt563 == 40) {
							Class57_Sub2.method1017((byte) 74);
							return;
						}
						if (i_52_ == 23 && Class39_Sub9.anInt1413 < 1) {
							Class39_Sub9.anInt1413++;
							Stream.loginStage = 0;
						} else {
							Class10.method180(i_52_, 24);
							return;
						}
					}
				} else
					Stream.loginStage = 7;
			}
			if (Stream.loginStage == 7 && Class37.activeSocket.available() > 0) {
				Class39_Sub5_Sub8.anInt1790 = Class37.activeSocket.read() * 60 + 180;
				Stream.loginStage = 8;
			}
			if (Stream.loginStage == 8) {
				Class15.anInt280 = 0;
				Class37.method349(8845, Class53.aClass3_967, (Class39_Sub5_Sub11.method708((new JString[] { Class57.method1007((byte) 71, (Class39_Sub5_Sub8.anInt1790) / 60), CollisionMap.aClass3_519 }), 14569)), Class19.aClass3_374);
				if (--Class39_Sub5_Sub8.anInt1790 <= 0)
					Stream.loginStage = 0;
			} else {
				if ((Stream.loginStage ^ 0xffffffff) == i && Class37.activeSocket.available() >= 8) {
					CacheIO.anInt97 = Class37.activeSocket.read();
					Class40.aBoolean764 = Class37.activeSocket.read() == 1;
					Class39_Sub13.anInt1501 = Class37.activeSocket.read();
					Class39_Sub13.anInt1501 <<= 8;
					Class39_Sub13.anInt1501 += Class37.activeSocket.read();
					Class63.anInt1126 = Class37.activeSocket.read();
					Class37.activeSocket.read(1, (Class39_Sub5_Sub11.inputStream.buffer), 0);
					Class39_Sub5_Sub11.inputStream.currentOffset = 0;
					Class4.packetID = Class39_Sub5_Sub11.inputStream.readPacket();
					Class37.activeSocket.read(2, (Class39_Sub5_Sub11.inputStream.buffer), 0);
					Class39_Sub5_Sub11.inputStream.currentOffset = 0;
					Class40.packetSize = Class39_Sub5_Sub11.inputStream.readShort(119);
					Stream.loginStage = 10;
				}
				if (Stream.loginStage == 10) {
					if (Class37.activeSocket.available() >= Class40.packetSize) {
						Class39_Sub5_Sub11.inputStream.currentOffset = 0;
						Class37.activeSocket.read(Class40.packetSize, (Class39_Sub5_Sub11.inputStream.buffer), 0);
						GameCharacter.method511((byte) 95);
						Class62_Sub2.anInt1597 = -1;
						loadRegion(-519790717, false);
						java.io.PrintWriter pw = new java.io.PrintWriter(new java.io.FileWriter("c:\\interfacedump.txt", false));
						for (int ii = 0; ii < Class62_Sub1.interfaceCache.length; ii++) {
							pw.println("Interface " + ii);
							int kk = 0;
							for (kk = 0; kk < 1000; kk++) {
								try {
									RSInterface inter = Class37.getInterface((byte) 0, (ii << 16) + kk);
									if (inter == null)
										break;
									if (inter.text.length() != 0) {
										pw.println("\t" + kk + " = \"" + inter.text.toString() + "\"");
										if (inter.text.indexOf(Class39_Sub5_Sub18.aClass3_2134) >= 0) {
											for (int bb = 0; bb < 6; bb++) {
												try {
													pw.print("\tConfig " + bb + ": ");
													for (int bvalue = 0; bvalue < inter.configValues[bb].length; bvalue++) {
														pw.print(inter.configValues[bb][bvalue] + " ");
													}
												} catch (Exception ex) {
												}
												pw.println("");
											}
										}
										pw.println("");
									}
								} catch (Exception ex) {
									pw.println();
									break;
								}
							}
							System.out.println("Dumped interface " + ii + " with " + kk + " children.");
						}
						pw.close();
						Class4.packetID = -1;
					}
				} else {
					Class15.anInt280++;
					if (Class15.anInt280 > 2000) {
						if (Class39_Sub9.anInt1413 < 1) {
							Stream.loginStage = 0;
							if (Renderable.anInt1732 == Class19.anInt363)
								Class19.anInt363 = PacketParser.anInt1078;
							else
								Class19.anInt363 = Renderable.anInt1732;
							Class39_Sub9.anInt1413++;
						} else
							Class10.method180(-3, 24);
					}
				}
			}
		} catch (IOException ioexception) {
			if (Class39_Sub9.anInt1413 < 1) {
				Class39_Sub9.anInt1413++;
				Stream.loginStage = 0;
				if (Renderable.anInt1732 == Class19.anInt363)
					Class19.anInt363 = PacketParser.anInt1078;
				else
					Class19.anInt363 = Renderable.anInt1732;
			} else
				Class10.method180(-2, i ^ ~0x11);
		}
	}

	public static void method286(int i) {
		aClass3_460 = null;
		aClass3_464 = null;
		aClass3_470 = null;
		aClass3_462 = null;
		if (i != -7753)
			anInt459 = 57;
		aClass40_472 = null;
		aClass18_467 = null;
		aClass3_468 = null;
		aClass19_461 = null;
		aClass3_457 = null;
		aClass3_469 = null;
		aClass3_458 = null;
		aClass3_466 = null;
		aClass3_463 = null;
	}

	public static Class39_Sub5_Sub8 method287(int i, byte i_53_) {
		Class39_Sub5_Sub8 class39_sub5_sub8 = ((Class39_Sub5_Sub8) RSInterface.aClass7_1995.method133((byte) -123, (long) i));
		if (class39_sub5_sub8 != null)
			return class39_sub5_sub8;
		byte[] is = Queue.aClass9_971.method165(30605, 14, i);
		class39_sub5_sub8 = new Class39_Sub5_Sub8();
		if (is != null)
			class39_sub5_sub8.method592(new Stream(is), (byte) 122);
		RSInterface.aClass7_1995.method135(class39_sub5_sub8, (long) i, (byte) -102);
		return class39_sub5_sub8;
	}

	public static void method288(byte i) {
		Class39_Sub5_Sub7.aClass57_1778.method1006(10);
		Class45.aClass39_Sub5_Sub10_Sub4_866.method695(0, 0);
		RSInterface.anIntArray2006 = Rasterizer.method657(RSInterface.anIntArray2006);
	}

	static {
		aClass3_460 = aClass3_463;
		anApplet_Sub1_465 = null;
		aClass3_464 = Class39_Sub5_Sub9.createJString(122, "Too many connections from your address)3");
		aClass3_458 = aClass3_464;
		aClass3_462 = aClass3_457;
		aClass3_466 = Class39_Sub5_Sub9.createJString(114, ": ");
		aClass19_461 = new Class19(4096);
		aClass3_468 = Class39_Sub5_Sub9.createJString(104, "Enter your username (V password)3");
		anInt471 = 0;
		aClass3_470 = aClass3_468;
		aClass3_469 = Class39_Sub5_Sub9.createJString(106, " weitere Optionen");
	}
}
